var models = require('../models/user.js');
var lession = require('../models/lession.js').lession;
var type = require('../models/lessiontype.js').lessiontype;
var moment = require('moment');
var comment = require('../models/comment.js').comment;
var ObjectId = require('mongoose').Types.ObjectId;
var menus = require('./menu').menu();
menus.lession.active = 'active';
module.exports.autoroute = {
	get:{
		'/admin/lession':function(req,res){
			res.render("lession",{menu:menus})
		},
		'/admin/lession/add':function(req,res){
			var id = req.query.id,_lession = {lessionname:"",_id:"",res:[],typename:"",typeid:"",img:""};
			if(id)
				lession.findById(ObjectId(id),function(err,result){
					type.findById(result.typeid,function(err,type){
						result.typename = type.name;
						res.render('lessionadd',{menu:menus,'lession':result});
					})
				});
			else
				res.render('lessionadd',{menu:menus,'lession':_lession});
		},
		'/admin/lession/delete':function(req,res){
			var callback = function(err){
			  if(err){
			    debug(err);
			    res.send({code:-1,message:'保存失败'})
			  }else
			    res.redirect("/admin/lession");
			};
			lession.findByIdAndRemove(ObjectId(req.query.id),callback);
		},
		'/admin/ajax/lession':function(req,res){
			var page = 0;
			if(req.query.iDisplayStart)
				page = req.query.iDisplayStart;
			condition = {};
			if(req.query.name)
				condition.name = new RegExp(req.query.name);
			lession.find(condition).skip(page).limit(10).lean(true).exec(function(err,user){
				lession.find().count(function(err,size){
					user.forEach(function(item){
						item.create_time = moment(item.create_time).format("YYYY-MM-DD HH:mm");
					});
		  			res.json({aaData:user,iTotalRecords:size,iTotalDisplayRecords:size});
				})
			});
		},
		'/admin/type/search':function(req,res){
			condition = {name:new RegExp(req.query.name)};
			type.find(condition,'name').exec(function(err,user){
		  		res.json(user);
			});
		},

		'/admin/lession/comment':function(req,res){
			lession.findById(ObjectId(req.query.id),function(err,lession){
				res.render("comment",{menu:menus,lession:lession});
			});
		},
		'/admin/lession/comment/delete':function(req,res){
			var callback = function(err){
			  if(err){
			    debug(err);
			    res.send({code:-1,message:'保存失败'})
			  }else
			    res.redirect("/admin/lession/comment?id="+req.query.lessionid);
			};
			comment.findByIdAndRemove(ObjectId(req.query.id),callback);
		},
		'/admin/ajax/lession/comment':function(req,res){
			var page = 0;
			if(req.query.iDisplayStart)
							page = req.query.iDisplayStart;
			condition = {};
			if(req.query.name)
				condition.name = new RegExp(req.query.name);
			if(req.query.lessionid)
				condition.lessionid = ObjectId(req.query.lessionid);
			comment.find(condition).skip(page).limit(10).lean(true).exec(function(err,user){
				comment.find().count(function(err,size){
					user.forEach(function(item){
						item.create_time = moment(item.create_time).format("YYYY-MM-DD HH:mm");
					});
		  			res.json({aaData:user,iTotalRecords:size,iTotalDisplayRecords:size});
				})
			});
		}
	},
	post:{
		'/admin/lession/save':function(req,res){
			var callback = function(err){
			  if(err){
			    debug(err);
			    res.send({code:-1,message:'保存失败'})
			  }else
			    res.redirect("/admin/lession");
			};

			var names = req.body.names;
			var paths = req.body.paths;
			var videos = [];
			for(var i in names){
				videos.push({name:names[i],path:paths[i]})
			}

			if(req.body.id){
				lession.findByIdAndUpdate(ObjectId(req.body.id),{'lessionname' : req.body.name, type : ObjectId(req.body.typeid),
					img : req.body.img,
				  desc : req.body.desc,
				  res:videos},callback);
			}
			else{
				var _lession = new lession({
				  'lessionname' : req.body.name, 
				  desc : req.body.desc,
				  img : req.body.img,
				  user : [],
				  typeid : ObjectId(req.body.typeid),
				  res:videos
				});
				_lession.save(callback);
			}
		}
	}
};

